/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.crystalrange;

import java.io.StringWriter;
import java.util.Map.Entry;
import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.*;
import org.zkoss.zk.ui.event.ClientInfoEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Html;
import org.zkoss.zul.Textbox;

/**
 *
 * @author tbekasiewicz
 */
public class SessionInfoController extends BaseController {

    protected Html htmlSessionInfo;
    protected ClientInfoEvent event;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        
        Properties properties = new Properties();
	properties.setProperty("file.resource.loader.path", this.desktop.getWebApp().getRealPath("/templates"));
	Velocity.init(properties);
    }

    public void onClick$toolbarbuttonShowInfo(Event event) throws Exception {
        this.collectInfo();
    }

    public void onClick$toolbarbuttonShowSecurityContext(Event event) throws Exception {
        this.collectSecurityContext();
    }

    public void onClick$toolbarbuttonShowSystemProperties(Event event) throws Exception {
        this.collectSystemProperties();
    }

    public void onClientInfo$windowSessionInfo(ClientInfoEvent event) throws Exception {
        this.event = event;
    }

    private void collectInfo() {
                
        VelocityContext context = new VelocityContext();
        context.put("currentSession", Sessions.getCurrent());
        context.put("currentExecution", Executions.getCurrent());
        context.put("event", this.event);
        
        StringWriter writer = new StringWriter();        
              
        Template template = Velocity.getTemplate("collectInfo.vsl");
        template.merge(context, writer);
        
        this.htmlSessionInfo.setContent(writer.toString());
    }
        
    private void collectSecurityContext() {
        
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
            
        VelocityContext context = new VelocityContext();
        context.put("authentication", authentication);
        
        StringWriter writer = new StringWriter();        
              
        Template template = Velocity.getTemplate("collectSecurityContext.vsl");
        template.merge(context, writer);
        
        this.htmlSessionInfo.setContent(writer.toString());
    }

    private void collectSystemProperties() {
        
        VelocityContext context = new VelocityContext();
        context.put("properties", System.getProperties());
        
        StringWriter writer = new StringWriter();        
              
        Template template = Velocity.getTemplate("collectSystemProperties.vsl");
        template.merge(context, writer);
        
        this.htmlSessionInfo.setContent(writer.toString());
    }
}
